package com.serkol.abmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.abmanager.bean.GridResult;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.Mechanisms;
import com.serkol.abmanager.service.MechanismsService;
import com.serkol.abmanager.mapper.MechanismsMapper;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class MechanismsServiceImpl extends ServiceImpl<MechanismsMapper, Mechanisms>
        implements MechanismsService {

    @Override
    public GridResult findByList(String name, int page, int pagesize) {

        GridResult gridResult = new GridResult();
        QueryWrapper<Mechanisms> queryWrapper = new QueryWrapper<>();
        if(!name.isEmpty()){
            queryWrapper.lambda().eq(Mechanisms::getName,name);
        }
        queryWrapper.lambda().orderByDesc(Mechanisms::getId);
        Page<Mechanisms> page0 = new Page<>(page, pagesize);
        IPage<Mechanisms> page1 = this.baseMapper.selectPage(page0, queryWrapper);
        if (page1.searchCount()) {
            long count = this.count(queryWrapper);
            gridResult.setPage(page1.getCurrent());
            gridResult.setTotal(count);
            int pagesize1 = Long.valueOf(count).intValue() / pagesize;
            if (pagesize1 <= 0) {
                pagesize1 = 1;
            }
            gridResult.setPages(pagesize1);
            gridResult.setData(page1.getRecords());
            gridResult.setStatus(ResultConstant.SUCSESS);
        } else {
            gridResult.setStatus(ResultConstant.EMPTIY);
        }

        return gridResult;
    }
}




